package com.dongdongshop.service.impl;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.model.TbSeller;
import com.dongdongshop.model.TbSellerExample;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.uitl.ShiroUtils;
import com.dongdongshop.vo.SellerVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {

    @Autowired
    private TbSellerMapper tbSellerMapper;

    @Override
    public SellerVo getSellerByName(String sellerId) {
        TbSeller tbSeller = tbSellerMapper.selectByPrimaryKey(sellerId);
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(tbSeller,sellerVo);
        return sellerVo;
    }

    @Override
    public SellerVo TbSellershowbysellerId(String sellerId) {
        TbSeller tbSeller=tbSellerMapper.selectByPrimaryKey(sellerId);
        SellerVo sellerVo = new SellerVo();
        BeanUtils.copyProperties(tbSeller, sellerVo);
        return sellerVo;

    }

    @Override
    public void updateTbSeller(SellerVo sellerVo) {
        TbSeller tbSeller = new TbSeller();
        BeanUtils.copyProperties(sellerVo, tbSeller);
        tbSellerMapper.selectByPrimaryKey(tbSeller.getSellerId());
        tbSeller.setStatus(tbSeller.getStatus());
        tbSellerMapper.updateByPrimaryKeySelective(tbSeller);
    }

    @Override
    public List<SellerVo> SellerList(SellerVo vo) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(vo.getName())){
            criteria.andNameLike("%"+vo.getName()+"%");
        }
        if (StringUtils.isNoneBlank(vo.getNickName())){
            criteria.andNameLike("%"+vo.getNickName()+"%");
        }

        List<TbSeller> tbBrands = tbSellerMapper.selectByExample(example);
        return tbBrands.stream().map(seller ->{
            SellerVo vv = new SellerVo();
            BeanUtils.copyProperties(seller,vv);
            return vv;
        }).collect(Collectors.toList());
    }

    @Override
    public List<TbSeller> showbysellerId(String sellerId) {
        TbSellerExample te = new TbSellerExample();
        TbSellerExample.Criteria criteria = te.createCriteria();
        criteria.andSellerIdEqualTo(sellerId);
        return tbSellerMapper.selectByExample(te);
    }

    @Override
    public void addTbSeller(SellerVo vo) {
        TbSeller tbSeller = new TbSeller();
        BeanUtils.copyProperties(vo,tbSeller);
        tbSellerMapper.insert(tbSeller);
    }

}
